/**
 * 
 */
package com.hww.DesignPattern.Factory;

import com.hww.sort.ISortNumber;
import com.hww.sort.impl.BubbleSort;
import com.hww.sort.impl.LinearInsertSort;
import com.hww.sort.impl.QuickSort;
import com.hww.sort.impl.SelectionSort;

/**
 * 模式名称：工厂模式 模式特征：通过一个通用的接口创建不同的类对象 模式用途：面向接口编程
 * 
 * @author:Huangwenwei
 * @date:2014-6-20
 * @time:下午7:13:10
 */
public class Factory {

	public static final String SELECTION_SORT = "selection";
	public static final String BUBBLE_SORT = "bubble";
	public static final String LINEARINSERT_SORT = "liner";
	public static final String QUICK_SORT = "quick";

	/**
	 * 创建具体排序算法（产品）的方法
	 * 
	 * @Time:下午7:19:50
	 * @Data:2014-6-20
	 * @Email:mr.huangwenwei@gmail.com
	 * @param id排序算法的具体类型
	 * @return 产品接口对象
	 * @Return:ISortNumber
	 */
	public static ISortNumber getOrderNumber(String id) {
		if (SELECTION_SORT.equalsIgnoreCase(id))
			return new SelectionSort();
		else if (BUBBLE_SORT.equalsIgnoreCase(id))
			return new BubbleSort();
		else if (LINEARINSERT_SORT.equalsIgnoreCase(id))
			return new LinearInsertSort();
		else if (QUICK_SORT.equalsIgnoreCase(id))
			return new QuickSort();
		else
			return null;
	}

	/**
	 * 打印排序之后的数组
	 * 
	 * @Time:下午7:25:09
	 * @Data:2014-6-20
	 * @Email:mr.huangwenwei@gmail.com
	 * @param array
	 * @Return:void
	 */
	public static void printIntArray(int[] array) {
		if (array != null) {
			for (int i = 0; i < array.length; i++) {
				System.out.print(array[i] + " ");
			}
			System.out.println();
		}
	}

	/**
	 * @Time:下午7:13:10
	 * @Data:2014-6-20
	 * @Email:mr.huangwenwei@gmail.com
	 * @param args
	 * @Return:void
	 */
	public static void main(String[] args) {
		int[] intarray = { 4, 9, 5, 2, 6, 9, 6, 4, 3 };
		System.out.print("排序前的数组是：");
		printIntArray(intarray);
		System.out.print("用选择排序法对数组进行升序排序后的结果是：");
		int[] orderedArray = Factory.getOrderNumber(Factory.SELECTION_SORT)
				.sortASC(intarray);
		printIntArray(orderedArray);

		System.out.print("用冒泡排序法进行升序排序后的结果是：");
		orderedArray = Factory.getOrderNumber(Factory.BUBBLE_SORT).sortASC(
				intarray);
		printIntArray(orderedArray);

		System.out.print("用线性插入排序法进行升序排序后的结果是：");
		orderedArray = Factory.getOrderNumber(Factory.LINEARINSERT_SORT)
				.sortASC(intarray);
		printIntArray(orderedArray);

		System.out.print("用快速排序法进行升序排序后的结果是：");
		orderedArray = Factory.getOrderNumber(Factory.QUICK_SORT).sortASC(
				intarray);
		printIntArray(orderedArray);
	}

}
